package com.example.zzm.yuejian03.Activity;


import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.support.annotation.Nullable;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.zzm.yuejian03.Bean.ClubBean;
import com.example.zzm.yuejian03.Bean.collectBean;
import com.example.zzm.yuejian03.Listener.TListener;
import com.example.zzm.yuejian03.Model.ClubModeldetails;
import com.example.zzm.yuejian03.Model.CollectclubModel;

import com.example.zzm.yuejian03.R;
import com.squareup.picasso.Picasso;

public class ClubActivity extends AppCompatActivity implements View.OnClickListener {

    private int resid;
    private Context context;
    private SharedPreferences sp;//简单存储
    private int userid;
    private TextView textView11,textView15,textView16,textView18,textView20,min,mop,textView22,textView23;
    private ImageView imageView9,mpic,image;
    private Button button9,button10,button8;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_club);
        //System.out.println("----查看俱乐部详情");
        //context=ClubActivity.this;
        resid  = getIntent().getIntExtra("club_id",1);//获取传递的资源id
        sp=getSharedPreferences("userInfo",MODE_PRIVATE);
        readSP();//读取sessionid

        initViews();
        initEvent();
        ClubModeldetails ClubModeldetails = new ClubModeldetails();
        ClubModeldetails.getClubdetailss(getIntent().getIntExtra("club_id",1),listener);

        CollectclubModel model=new CollectclubModel();//实例化对象
        model.islike(resid,userid,tlistener);//判断是否收藏


    }
    private void readSP() {
        userid = sp.getInt("user_id",1);
    }

    TListener<ClubBean> listener = new TListener<ClubBean>() {
        @Override
        public void onResponse(ClubBean club) {
            textView15.setText(club.getClub_name());
            textView16.setText(club.getIntro());
            textView20.setText(Integer.toString(club.getPhone()));
            textView18.setText(club.getMaster_name());
            min.setText(club.getMaster_intro());
            mop.setText(club.getOpentime());
            Picasso.with(ClubActivity.this)
            .load("http://10.0.2.2/yuejian10/public/static/asstes/images/" + club.getMaster_pic())
                    .placeholder(R.mipmap.ic_launcher)
                    // .memoryPolicy(MemoryPolicy.NO_CACHE)
                    .into(mpic);
            Picasso.with(ClubActivity.this)
                    .load("http://10.0.2.2/yuejian10/public/static/asstes/images/" + club.getPic())
                    .placeholder(R.mipmap.ic_launcher)
                    // .memoryPolicy(MemoryPolicy.NO_CACHE)
                    .into(imageView9);
        }
        @Override
        public void onFail(String msg) {

        }
    };


    private void initViews(){
        textView15 = (TextView)findViewById(R.id.textView15);//mingc
        textView16 = (TextView)findViewById(R.id.textView16);//jieshao
        textView18= (TextView)findViewById(R.id.textView18);//jiaolian
        textView20 = (TextView)findViewById(R.id.textView20);
        min = (TextView)findViewById(R.id.min);
        mop = (TextView)findViewById(R.id.mop);
        mpic=(ImageView)findViewById(R.id.mpic);
        imageView9 = (ImageView)findViewById(R.id.imageView9);
        textView11 = (TextView) findViewById(R.id.textView11);//fanhui
        button8 = (Button)findViewById(R.id.button8);
        button9 = (Button)findViewById(R.id.button9);//kec
        button10 = (Button)findViewById(R.id.button10);
    }

    TListener<collectBean> ttlistener=new TListener<collectBean>() {
        @Override
        public void onResponse(collectBean collectBean) {
            if (collectBean.getError().equals("0")){
                Toast.makeText(ClubActivity.this,"关注成功！", Toast.LENGTH_SHORT).show();
            }else {
                Toast.makeText(ClubActivity.this,"关注失败！", Toast.LENGTH_SHORT).show();
            }
        }
        @Override
        public void onFail(String msg) {
        }
    };
    TListener<collectBean> tlistener=new TListener<collectBean>() {
        @Override
        public void onResponse(collectBean collectBean) {
            if (collectBean.getError().equals("0")){
                button8.setText("取消关注");
            }else {
                button8.setText("关注");
            }
        }
        @Override
        public void onFail(String msg) {
        }
    };
    TListener<collectBean> tttlistener=new TListener<collectBean>() {
        @Override
        public void onResponse(collectBean collectBean) {
            if (collectBean.getError().equals("0")){
                Toast.makeText(ClubActivity.this,"取消关注成功", Toast.LENGTH_SHORT).show();
            }else {
                Toast.makeText(ClubActivity.this,"取消关注失败", Toast.LENGTH_SHORT).show();
            }
        }
        @Override
        public void onFail(String msg) {
        }
    };
    private void initEvent(){
        textView11.setOnClickListener(this);
        button8.setOnClickListener(this);
        button10.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(ClubActivity.this,MovieActivity.class);
                intent.putExtra("club_id",getIntent().getIntExtra("club_id",1));
                startActivity(intent);
            }
        });
        button9.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(ClubActivity.this,ClassActivity.class);
                intent.putExtra("club_id",getIntent().getIntExtra("club_id",1));
                startActivity(intent);
            }
        });
    }

    @Override
    public void onClick(View v) {
        CollectclubModel model=new CollectclubModel();//实例化对象
        switch (v.getId()){
            case R.id.textView11:
                finish();
                break;
            case R.id.button8:
                if(button8.getText().equals("关注"))//如果已收藏，则调用取消收藏
                {
                    model.Like(resid,userid,ttlistener);
                    button8.setText("取消关注");
                }
                else//如果未收藏，则调用收藏
                {
                    model.unLike(resid,userid,tttlistener);
                    button8.setText("关注");
                }
                break;
        }
    }

}
